package com.itheima.web.controller.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.service.sms.BrandService;
import com.itheima.utils.PageResult;
import com.itheima.utils.PageUtils;
import com.itheima.utils.ResultData;
import com.itheima.vo.sms.Brand;
import com.itheima.vo.sms.BrandVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/home/brand")
public class BrandController {
    @DubboReference
    private BrandService brandService;

    /**
     * 添加首页推荐品牌
     * @param brands
     * @return
     */
    @PostMapping("/create")
    public ResultData create(@RequestBody List<Brand> brands){
        brandService.saveBatch(brands);
        return ResultData.ok("添加成功",null);
    }

    /**
     * 批量删除推荐品牌
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ResultData delete(@RequestParam List ids){
        brandService.removeByIds(ids);
        return ResultData.ok("删除成功",null);
    }

    /**
     * 分页查询推荐品牌
     * @param brandName
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String brandName,Integer pageNum, Integer pageSize, Integer recommendStatus) {

        Page<Brand> brandPage = brandService.getpage(pageNum,pageSize,brandName,recommendStatus);

        PageResult page = PageResult.getPage(brandPage);

        return ResultData.ok("查询成功",page);
    }

    /**
     * 批量修改推荐品牌状态
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public ResultData updateRecommendStatusByIds(Integer recommendStatus,Integer[] ids){

        brandService.updateByStatus(Arrays.asList(ids),recommendStatus);
        return ResultData.ok("修改成功",null);
    };
    /**
     * 修改推荐品牌排序
     * @param id
     * @param sort
     * @return
     */
    @PostMapping("update/sort/{id}")
    public ResultData updateSortById(@PathVariable Integer id, Integer sort){
        Brand brand=brandService.getById(id);
        brand.setSort(sort);
        brandService.updateById(brand);
        return ResultData.ok("修改成功",null);
    }
}